// pages/order/order_list.js
var api = require('../../utils/api');
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    searchOrder:{
      orderNo:"",
      orderStatus: ['0', '1', '2', '3', '4'],
      createId:"",
      id: ""
    },
    /** 
        * 页面配置 
        */
    winWidth: 0,
    winHeight: 0,
    // tab切换  
    currentTab: 0,
  },  

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
   
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  onLoad: function () {
    console.log(app.globalData);
    var createId = app.globalData.opendId;
    this.data.searchOrder.createId = createId;
    this.setAdaption();
    this.getOrderlist();
  },
  setAdaption:function(){
    var that = this;
    /** 
    * 获取系统信息 
    */
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          winWidth: res.windowWidth,
          winHeight: res.windowHeight
        });
      }
    });
  },
  getOrderlist: function (){
     var url = api.getMyOrder();
     var params = this.data.searchOrder;
     var that = this;
    
    api.request(url, 'POST', params, function (res, err) {
      that.setData({ orderlist: res });
    });
  },
  /** 
     * 滑动切换tab 
     */
  bindChange: function (e) {
    var that = this;
    that.setData({ currentTab: e.detail.current });

  },  
  /** 
   * 点击tab切换 
   */
  swichNav: function (e) {
    var orderStatus = e.currentTarget.dataset.status;
    if (orderStatus==""){
      this.data.searchOrder.orderStatus = ['0', '1', '2', '3', '4'];
    } else if (orderStatus=="1"){
      this.data.searchOrder.orderStatus = ['1'];
    } else if (orderStatus == "2") {
      this.data.searchOrder.orderStatus = ['2'];
    } else if (orderStatus == "0") {
      this.data.searchOrder.orderStatus = ['0', '2', '3', '4'];
    } else if (orderStatus == "3") {
      this.data.searchOrder.orderStatus = ['3','4'];
    }
    
    this.getOrderlist();

    var that = this;

    if (this.data.currentTab === e.target.dataset.current) {
      return false;
    } else {
      that.setData({
        currentTab: e.target.dataset.current
      })
    }
  }, orderDetail: function (e) {
    var id = e.currentTarget.dataset.id;
    var orderNo = e.currentTarget.dataset.orderno;
    var orderStatus = e.currentTarget.dataset.orderstatus;

    //待支付的跳转到支付页面
    if ("0" == e.currentTarget.dataset.orderstatus){
      wx.navigateTo({
        url: '../payment/payment?orderNo=' + orderNo
      });
    }else{
      wx.navigateTo({
        url: '../orderresult/orderresult?id=' + id + "&orderNo=" + orderNo
      });
    }
   
  },reflush:function(){
    this.getOrderlist();
  }
})